// 导入axios
import axios from 'axios';
import qs from 'qs'

// 进行一些全局配置
var baseURL = 'http://153.99.98.90:8083'
// var baseURL = 'http://127.0.0.1:8089'
// var baseURL = 'http://192.168.68.201:8089'
// var baseURL = 'http://139.196.105.243:8089'
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 公共路由(网络请求地址)
axios.defaults.baseURL = baseURL;
// axios.defaults.baseURL = 'http://192.168.68.138:8089';
// axios.defaults.baseURL = 'https://api.qqsuu.cn/api/weather';
// 请求响应超时时间
axios.defaults.timeout = 5000;
// token
// axios.defaults.headers.common['token'] = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsaXV5dWFuIiwicGFzc3dvcmQiOiJ5aXl1bi4yMDIwIiwibG9naW5OYW1lIjoibGl1eXVhbiIsImlhdCI6MTYwMDkyMjE1NiwianRpIjoiNjJhNTVlYWEtNjY2Yi00ZGE0LTlhZDEtN2I1NzY2OTc3OTQyIn0.HP4_uJ_aL04IBe2dDeiMF5T6J8wTtuxDlIikg2hK6Hc"

// 天气专用
const weatherHttp = axios.create({
  baseURL: 'https://devapi.qweather.com',
  // &key=452b15f12c42461694522e559ae4ed93
});

// 封装 get/post方法
export default {
  get: function(path = '', data = {}) {
    return new Promise(function(resolve, reject) {
      axios.get(path, {
        params: data
      })
        .then(function(response) {
          resolve(response);
        })
        .catch(function(error) {
          reject(error);
        });
    });
  },
  post: function(path = '', data = {}) {
    // console.log('post提交的参数：');
    // console.log(data);
    return new Promise(function(resolve, reject) {
      axios.post(path, qs.stringify(data))
        .then(function(response) {
          resolve(response)
        })
        .catch(function(error) {
          reject(error)
        });
    });
  },
  getWeather: function(path = '', data = {}) {
    return new Promise(function(resolve, reject) {
      weatherHttp.get(path, {
        params: data
      })
        .then(function(response) {
          resolve(response);
        })
        .catch(function(error) {
          reject(error);
        });
    });
  },
  baseURL
};
